<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"fengxun2017.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.13.2","exturl":false,"sidebar":{"position":"right","display":"hide","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="性能优化核心思想，个人笔记。">
<meta property="og:type" content="article">
<meta property="og:title" content="性能优化核心思想：个人笔记">
<meta property="og:url" content="http://fengxun2017.github.io/2023/04/15/performance-optimization-sunnmary/index.html">
<meta property="og:site_name" content="fx">
<meta property="og:description" content="性能优化核心思想，个人笔记。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-04-14T16:00:00.000Z">
<meta property="article:modified_time" content="2023-04-16T06:03:13.031Z">
<meta property="article:author" content="feng xun">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://fengxun2017.github.io/2023/04/15/performance-optimization-sunnmary/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://fengxun2017.github.io/2023/04/15/performance-optimization-sunnmary/","path":"2023/04/15/performance-optimization-sunnmary/","title":"性能优化核心思想：个人笔记"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>性能优化核心思想：个人笔记 | fx</title>
  






  <script async defer data-website-id="" src=""></script>

  <script defer data-domain="" src=""></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
  <a target="_blank" rel="noopener" href="https://github.com/fengxun2017" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">fx</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-%E5%9F%BA%E6%9C%AC%E4%BB%A3%E7%A0%81%E5%B1%82%E9%9D%A2%EF%BC%9A"><span class="nav-number">1.</span> <span class="nav-text">1 基本代码层面：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8%E6%A1%86%E6%9E%B6%E3%80%81%E4%B8%AD%E9%97%B4%E4%BB%B6%E5%B1%82%E9%9D%A2%EF%BC%9A"><span class="nav-number">2.</span> <span class="nav-text">2 使用框架、中间件层面：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-%E7%B3%BB%E7%BB%9F%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1%E5%B1%82%E9%9D%A2%EF%BC%9A"><span class="nav-number">3.</span> <span class="nav-text">3 系统架构设计层面：</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-1-%E5%90%88%E7%90%86%E7%9A%84%E6%A8%A1%E5%9D%97%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB%EF%BC%9A"><span class="nav-number">3.1.</span> <span class="nav-text">3.1 合理的模块依赖关系：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-2-%E5%B9%B6%E8%A1%8C%E5%A4%84%E7%90%86%EF%BC%9A"><span class="nav-number">3.2.</span> <span class="nav-text">3.2 并行处理：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-3-%E4%BD%BF%E7%94%A8%E7%BC%93%E5%AD%98%EF%BC%9A"><span class="nav-number">3.3.</span> <span class="nav-text">3.3 使用缓存：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-4-%E6%B1%A0%E5%8C%96%EF%BC%9A"><span class="nav-number">3.4.</span> <span class="nav-text">3.4 池化：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-5-%E5%BC%82%E6%9E%84%E8%AE%A1%E7%AE%97%EF%BC%9A"><span class="nav-number">3.5.</span> <span class="nav-text">3.5 异构计算：</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">feng xun</p>
  <div class="site-description" itemprop="description">FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">96</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">16</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://fengxun2017.github.io/2023/04/15/performance-optimization-sunnmary/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="feng xun">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="fx">
      <meta itemprop="description" content="FreeRTOS交流QQ群-663806972 BLE学习交流群-498676838">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="性能优化核心思想：个人笔记 | fx">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          性能优化核心思想：个人笔记
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-04-15 00:00:00" itemprop="dateCreated datePublished" datetime="2023-04-15T00:00:00+08:00">2023-04-15</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" itemprop="url" rel="index"><span itemprop="name">性能优化</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.1k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>1 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <center>
性能优化核心思想，个人笔记。
</center>

<span id="more"></span>

<hr>
<p>个人笔记，内容大部分来自<a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1MX4y1o7My/?spm_id_from=333.337.search-card.all.click&vd_source=fb4ef4d1b0e426d02542e32563feb87f">架构技术：性能优化的核心思想</a>。</p>
<h4 id="1-基本代码层面："><a href="#1-基本代码层面：" class="headerlink" title="1 基本代码层面："></a>1 基本代码层面：</h4><ul>
<li>合理的数据结构，合理的算法复杂度。 例如，一批常驻内存的数据的高频查找功能。使用hash数据结构比数组的效率更高。</li>
<li>熟悉工作语言的相关库，例如，<code>C++ STL</code>的<code>map</code>和<code>unordered_map</code>，需要根据自己的使用场景特性，选择合适的结构。</li>
</ul>
<h4 id="2-使用框架、中间件层面："><a href="#2-使用框架、中间件层面：" class="headerlink" title="2 使用框架、中间件层面："></a>2 使用框架、中间件层面：</h4><ul>
<li>需要理解所使用框架的相关配置：例如一些数据传输前的压缩处理（CPU充足，数据量大但网络传输资源不足的条件下）</li>
<li>使用方式选择：网络通信时用长连接短（一般内网服务间相互通信使用长连接）还是短连接（一般对外的访问接口使用短连接）</li>
<li>连接池的使用，同步通信和异步通讯的选择。</li>
</ul>
<h4 id="3-系统架构设计层面："><a href="#3-系统架构设计层面：" class="headerlink" title="3 系统架构设计层面："></a>3 系统架构设计层面：</h4><h5 id="3-1-合理的模块依赖关系："><a href="#3-1-合理的模块依赖关系：" class="headerlink" title="3.1 合理的模块依赖关系："></a>3.1 合理的模块依赖关系：</h5><p>一个请求到达后，经过的所有模块（或微服务）是否达到了最简？</p>
<ul>
<li>例如，一些后置处理是否可以延后执行？将结果先临时写记录，累计到一定数据再进行批量后处理。</li>
<li>例如，同步写日志操作，改为异步写方式</li>
<li>例如，使用同步接口通知其它服务某个事件，并等待返回的操作。是否可以改为异步接口通知？</li>
<li>例如，对于不需要立即处理的请求，可以将请求数据发送到消息队列系统，下游服务自己在消息队列系统中订阅相关消息。（可以带来功能解耦的额外好处，并且对于请求流量存在高低峰值的场景也可以抹平流量，合理充分的利用资源）</li>
</ul>
<h5 id="3-2-并行处理："><a href="#3-2-并行处理：" class="headerlink" title="3.2 并行处理："></a>3.2 并行处理：</h5><p>例如，加载一个大文件内容到内存，可以通过多线程并行，每个线程读取文件部分数据（通过fstream对象的seekg定位到不同的起始读位置）。<br>例如，当请求到达后，开启其它线程去执行相关工作。本线程继续等待其它请求。<br>PS：CPU的核心数有限，服务器上一般会存在各种服务应用程序在运行。因此，对于计算密集型任务，并行度不应该太高。否则（大于CPU核心数），线程间就要频繁切换，反而造成CPU利用效率变低（即看起来是CPU利用率变高了。但频繁的任务切换，本身也需要CPU资源，导致CPU实际执行真正工作的时间的占比反而降低）。</p>
<h5 id="3-3-使用缓存："><a href="#3-3-使用缓存：" class="headerlink" title="3.3 使用缓存："></a>3.3 使用缓存：</h5><p>根据需要建立不同级别的缓存。<br>例如,大规模的数据查找一般会有分层结构，如第一层是内存级别的缓存，下一层是固态硬盘缓存，最下一层是机械磁盘。 速度逐渐降低，但成本也更低。这种分层下，内存缓存基本可以直接命中高频热点请求，低频请求可以在固态硬盘缓存中命中，只有很少量的长尾请求才会需要去最底层的机械硬盘上查找。</p>
<h5 id="3-4-池化："><a href="#3-4-池化：" class="headerlink" title="3.4 池化："></a>3.4 池化：</h5><p>连接池、线程池，所有创建成本高的资源或对象，都可以通过池化的方式提前创建好。</p>
<h5 id="3-5-异构计算："><a href="#3-5-异构计算：" class="headerlink" title="3.5 异构计算："></a>3.5 异构计算：</h5><p>深度学习应用，可以使用GPU加速运算。<br>CPU上也可以看是否可以使用单指令多数据流（SIMD）来提高计算速度。</p>

    </div>

    
    
    

    <footer class="post-footer">
          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>feng xun
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://fengxun2017.github.io/2023/04/15/performance-optimization-sunnmary/" title="性能优化核心思想：个人笔记">http://fengxun2017.github.io/2023/04/15/performance-optimization-sunnmary/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/04/03/BleStack-link-packet/" rel="prev" title="实现一个精简的BLE从机协议栈—2—链路层包格式和nrf52840上的配置">
                  <i class="fa fa-chevron-left"></i> 实现一个精简的BLE从机协议栈—2—链路层包格式和nrf52840上的配置
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/04/24/BleStack-link-advertising/" rel="next" title="实现一个精简的BLE从机协议栈—3—链路层广播状态机实现">
                  实现一个精简的BLE从机协议栈—3—链路层广播状态机实现 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">feng xun</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">552k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">11:30</span>
  </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
